﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TrainingDemo.Model;
using TrainingDemo.Util.NativeUtil;

namespace TrainingDemo
{
    public partial class frmMain : Form
    {

        #region Construction/Destruction/Initialize

        public frmMain()
        {
            InitializeComponent();
        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            onLoad();
        }

        private void onLoad()
        {
            DesktopBinding();
            TechnologyBinding();
            DisplayInformation();

            cmbTechnology.Visible = false;
            lblTechnology.Visible = false;
            btnIntake.Visible = false;
        }

        private void DesktopBinding()
        {
            tvWindows.Nodes.Clear();

            List<IntPtr> lstTopMost = new List<IntPtr>();
            NativeUtils.getListDesktopWindows(IntPtr.Zero, ref lstTopMost, true);
            foreach (IntPtr pHandle in lstTopMost)
            {
                TreeNode pWindow = new WindowsNode(pHandle, NativeUtils.GetWindowTitle(pHandle, 255));
                tvWindows.Nodes.Add(pWindow);
                //tvWindows.Nodes.Add(NativeUtils.GetWindowTitle(pHandle, 255));
            }

            tvWindows.Refresh();
        }

        private void ControlBinding()
        {

        }


        private void TechnologyBinding()
        {

            // bind technology into cmbTechnology
            List<TechnologyBridge> lstTechnologies = new List<TechnologyBridge> {
                TechnologyHook.getInstance(),    
                TechnologyAccessibility.getInstance(), 
                TechnologyJavaReflection.getInstance(),
                TechnologyNetReflection.getInstance(),
                TechnologyInjectCode.getInstance()
                };


            cmbTechnology.DataSource = lstTechnologies;
            cmbTechnology.DisplayMember = "Name";
            cmbTechnology.SelectedIndex = -1;
        }

        #endregion


        #region Private Property

        List<ControlWrapper> m_lstControl;

        #endregion


        #region Public Methods




        #endregion


        #region Private Methods

        void DisplayInformation()
        {
            lblWindowsNumber.Text = tvWindows.GetNodeCount(true).ToString();
            lblControlNumber.Text = tvControls.GetNodeCount(true).ToString();
        }


        #endregion


        #region EventHandler

        private void tvWindows_AfterSelect(object sender, TreeViewEventArgs e)
        {
            WindowsNode pNode = (WindowsNode)e.Node;
            //MessageBox.Show("Window " + pNode.Handle.ToInt32().ToString() + " has title " + pNode.Title + " is selected");

            lblTechnology.Visible = true;
            cmbTechnology.Visible = true;
            btnIntake.Visible = true;
        }

        private void btnIntake_Click(object sender, EventArgs e)
        {
            string strInfor = tvWindows.SelectedNode.Text;
            if (strInfor.Equals(String.Empty))
            {
                MessageBox.Show("Please select at least 1 windows");
                return;
            }
            
            if (cmbTechnology.SelectedIndex == -1)
            {
                MessageBox.Show("Please select at least 1 technology");
                return;
            }
            
            if (strInfor.Length > 20)
            {
                strInfor = strInfor.Substring(0, 20) + "...";
            }
            lblListControl.Text = "List control of window " + strInfor;

            // intake control based on selected technology
            m_lstControl = new List<ControlWrapper>();
            IntakeControl(cmbTechnology.SelectedItem, tvWindows.SelectedNode.Handle, ref m_lstControl);

            // Display list of control


        }

        private void IntakeControl(object objTechnology, IntPtr pHandle, ref List<ControlWrapper> lstControl)
        {
            TechnologyBridge pTechnology = objTechnology as TechnologyBridge;

        }

        #endregion



    }
}
